<?php
namespace app\index\model;

use app\common\model\FormworkOrder as FormworkOrderModel;
use think\Cache;
use think\Db;

/**
 * 模板订单模型
 */
class FormworkOrder extends FormworkOrderModel
{
	/**
     * 待支付订单详情
     */
    public function payDetail($order_no)
    {
        return Cache::pull($order_no);
    }

	/**
     * 新增订单
     */
    public function updatePayStatus($transaction_id,$order)
    {
		if(!isset($order['detail'])){
			return true;//避免重复回调
		}
        // 开启事务
        Db::startTrans();
        try {
        	// 更新订单状态
			$this->save([
				'order_no' => $order['order_no'],
				'formwork_id' => $order['detail']['id'],
				'shop_type' => $order['shop_type'],
				'pay_price' => $order['total_fee'],
				'pay_status' => 20,
				'pay_time' => time(),
				'transaction_id' => $transaction_id,
				'user_id' => $order['user_id']
			]);
			//更新销售量
			Formwork::where('id', $order['detail']['id'])
			    ->inc('sale_actual',1)
     			->update();
			//更新积分
			User::where('id', $order['user_id'])
			    ->inc('score',$order['total_fee'])
     			->update();
			Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
        }
        return false;
    }
}
